<?php 

namespace login\view;

require_once("common/model/User.php");
require_once("login/model/LoginModel.php");

class LoginView {

	private static $LOGOUT = "logout";
	private static $USERNAMECOOKIE = "USERNAME";
	private static $PASSWORDCOOKIE = "PASSWORD";
	private static $USERNAME = "LoginView::Username";
	private static $PASSWORD = "LoginView::Password";
	private static $SUBMIT = "LoginView::Submit";
	private static $REMEMBER = "LoginView::Remember";
	/**
	 * @var \login\model\LoginModel $model
	 */
	private $model;

	/**
	 * @var string $message
	 */
	public $message;


	public function __construct(\login\model\LoginModel $loginModel) {

		$this->model = $loginModel;
	}

	public function loginHTMLForm() {
		$username = $this->getUsername();
		$html = 
		"<div id='loginForm'>
			<form action='?login' method='post' enctype='multipart/form-data'>				
				<fieldset>		
					<legend>Login</legend>
					<p>$this->message</p>
					<input id='UsernameID' placeholder='Username' value='$username' name='".self::$USERNAME."' /> 
					<input id='PasswordID' placeholder='Password' type='password' value='' name='".self::$PASSWORD."' />					
					<lable for='checkboxID'>
						<input id='checkboxID' type='checkbox' name='".self::$REMEMBER."' value='1'/>
						Remember me
					</lable>
					<input id='SubmitID' type='submit' value='Login' name='".self::$SUBMIT."' /> 
				</fieldset>
			</form>
		</div>";
		return $html;
	}

	public function getUserData() {
		if ($this->userCookies()) {
			$username = $_COOKIE[self::$USERNAMECOOKIE];
			$password = $_COOKIE[self::$PASSWORDCOOKIE];
			return new \common\model\User($username, $password);
		}
		else {
			$username = $this->getUsername();
			$password = $this->model->hashPassword($this->getPassword());
			return new \common\model\User($username, $password);
		}		
	}	

	private function rememberLogin() {
		if (isset($_POST[self::$REMEMBER])) {
			return true;
		}
	}

	public function logout() {

		$this->removeCookies();
	}

	private function removeCookies() {
		setcookie(self::$USERNAMECOOKIE, "", time()-10);
		setcookie(self::$PASSWORDCOOKIE, "", time()-10);
	}

	public function loggintOut() {

		return isset($_GET[self::$LOGOUT]);
	}

	public function loginSuccess() {
		$username = $this->getUsername();
		$password = $this->model->hashPassword($this->getPassword());
		if ($this->rememberLogin()) {
			setcookie(self::$USERNAMECOOKIE, $username, $this->model->cookieExperation());
			setcookie(self::$PASSWORDCOOKIE, $password, $this->model->cookieExperation());
		}		
	}

	public function loginFail() {
		if ($this->userCookies()) {
			$this->message = "<p>Incorrect information in cookie</p>";
			$this->removeCookies();
		} else {
			if ($this->getUsername() == "") {
				$this->message = "<p>Username missing</p>";
			} else if ($this->getPassword() == "") {
				$this->message = "<p>Password missing</p>";
			} else {
				$this->message = "<p>Wrong username and/or password</p>";
			}
		}
	}

	public function getLogoutButton() {
		$html = "<div id='logOutButton'>
					<a class='footerButton' href='?".self::$LOGOUT."'>Logout</a>
				 </div>";
		return $html;
	}

	public function userLoggingIn() {
		if (isset($_POST[self::$SUBMIT])) {
			return true;
		}
		return false;
	}

	public function userCookies() {
		if (isset($_COOKIE[self::$USERNAMECOOKIE]) && isset($_COOKIE[self::$PASSWORDCOOKIE])) {
			return true;
		}
		return false;
	}

	private function getUsername() {
		if (isset($_POST[self::$USERNAME])) {
			return $_POST[self::$USERNAME];
		}
	}

	private function getPassword() {
		if (isset($_POST[self::$PASSWORD])) {
			return $_POST[self::$PASSWORD];
		}
	}
}